package com.yoyoyo666.report.controller;

import com.github.pagehelper.StringUtil;
import com.yoyoyo666.report.common.*;
import com.yoyoyo666.report.mapper.ReportPlanMapper;
import com.yoyoyo666.report.mapper.ReportUserMapper;
import com.yoyoyo666.report.model.LoginUser;
import com.yoyoyo666.report.model.ReportPlan;
import com.yoyoyo666.report.model.common.BaseResult;
import com.yoyoyo666.report.model.common.ListResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("plan")
public class PlanController {


    @Autowired
    private ReportPlanMapper planMapper;

    @Autowired
    private ReportUserMapper reportUserMapper;


    @GetMapping("getAll")
    public ListResult<ReportPlan> getContent(@AnnoLogin LoginUser user){
       return  ListResult.getSuccess( planMapper.selectAll(user.getCurrentUser().getId(),null));
    }

    @PostMapping("getAllPage")
    public EasyUIPageResult<ReportPlan> getContent(@AnnoLogin LoginUser user, @RequestParam("page") String page, @RequestParam("rows") String row){
        ReportPage reportPage = new ReportPage(Long.valueOf(page), Long.valueOf(row));
        String userId = user.getCurrentUser().getId();
        List<ReportPlan> reportContents = planMapper.selectAll(userId, reportPage);
        reportPage.setRecords(reportContents);
        reportPage.setTotal(planMapper.selectAllCount(userId));
        return reportPage.getEuiPage();
    }

    @PostMapping("add")
    public BaseResult add(@AnnoLogin LoginUser user , ReportPlan content){
        int insert = saveContent(user, content);
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }
    }



    private int saveContent(@AnnoLogin LoginUser user, ReportPlan content) {
        content.setUserId(user.getCurrentUser().getId());
        content.setId(IDUtils.getId());
        content.setUpdateTime(DateUtils.getNowString());
        content.setCreateTime(DateUtils.getNowStringShort());
        return planMapper.insert(content);
    }


    @PostMapping("modify")
    public BaseResult modify(@AnnoLogin LoginUser user ,ReportPlan content){
        BaseResult fail = BaseResult.getFail();
        //content.setUpdateTime(DateUtils.getNowString());
        if(StringUtil.isEmpty(content.getId())){
            fail.setMsg("id不能为空");
            return fail;
        }
        int insert = planMapper.updateByPrimaryKeySelective(content);
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }
    }

    @PostMapping("delete")
    public BaseResult delete(@AnnoLogin LoginUser user ,ReportPlan content){
        BaseResult fail = BaseResult.getFail();
        if(StringUtil.isEmpty(content.getId()) ){
            fail.setMsg("id不能为空");
            return fail;
        }
        List<String> ids = new ArrayList<>();
        String[] split = content.getId().split(",");
        for (String s : split) {
            if(StringUtils.isBlank(s)){
                continue;
            }
            ids.add(s);
        }
        if(ids.size()==0){
            return BaseResult.getSuccess();
        }
        int insert = planMapper.deleteByPrimaryKeys(ids);
        if(insert>0){
            return BaseResult.getSuccess();
        }else{
            return BaseResult.getFail();
        }
    }

}
